<extend name="layout/main" />

<block name="HEADER">
    <style type="text/css">
    	.layui-tab-brief{ position: relative; }
		.layui-tab-brief > .btnAdd{
			position: absolute;
			right: 100px;
			top: 5px;
			cursor: pointer;
			z-index: 1;
		}
		.layui-tab-brief > .btnDel{
			position: absolute;
			right: 20px;
			top: 5px;
			cursor: pointer;
			z-index: 1;
		}
		.layui-tab { height: 100%; }
		.layui-tab .layui-tab-content { overflow: auto; padding: 25px; height: calc(100% - 40px); }
		.dlList { margin-bottom: 22px; overflow: hidden; }
		.dlList dt {
			border-bottom: 1px solid #eaeaea;
			margin-bottom: 10px;
			padding-bottom: 5px;
			color: #000000;
			font-weight: bold;
		}
		.dlList dd {
			padding-left: 10px;
			float: left;
			margin-right: 50px;
			line-height: 24px;
			cursor: pointer;
			margin-bottom: 15px;
			width: 20%;
		}
		.dlList dd p { color: #219AFF; font-weight: bold; }
		.dlList dd span { color: #CCCCCC; }
		/*轮播图*/
		.fileUploadContainer{ height: 100%; }
		.fileUpload-layui-carousel{ height: calc(100% - 25px) !important; }
    </style>
</block>

<block name="CONTENT">
	<php> $list = action('admin/soft/param/getParam'); $listIndex = 0; $listIndex2=0;</php>
	
	<div class="layui-tab  layui-tab-brief" >
		<button class="layui-btn layui-btn-normal layui-btn-sm btnAdd">新增参数</button>
		<button class="layui-btn layui-btn-normal layui-btn-sm btnDel">参数列表</button>
        <ul class="layui-tab-title">
        	<volist name=":$list" id="vo" key="k">
        	    <if condition="array_keys($vo)[0] != '店铺参数'">
        	    	<php>$listIndex ++;</php>
        		  	<li class="{$listIndex == 1 ? 'layui-this' : '' }">{:array_keys($vo)[0]}</li>
        		</if>
			</volist>
        </ul>
        <div class="layui-tab-content">
        	<volist name=":$list" id="vo" key="k" >
        	    <if condition="array_keys($vo)[0] != '店铺参数'">
        	    	<php>$listIndex2 ++;</php>
    				<div class="layui-tab-item {$listIndex2 ==1 ? 'layui-show' : '' }">
    					<volist name=":$vo[array_keys($vo)[0]]" id="v1" >
    						
    						<dl class="dlList">
    							<if condition="array_keys($v1)[0]">
    								<dt>{:array_keys($v1)[0]}</dt>
    							</if>
    							<volist name=":$v1[array_keys($v1)[0]]" id="v2" >
    								<dd data-data={:json_encode($v2,JSON_UNESCAPED_UNICODE);} >
    									<p>{$v2['param_name'] }</p>
    									<span>{$v2['param_des'] }</span>
    								</dd>
    							</volist>
    						</dl>
    						
    					</volist>
    				</div>
				</if>
			</volist>
        </div>
    </div>
    
    <!--下拉新增-->
    <script id="tpl-selectType" type="text/html">
        <form class="layui-form J-selectType">
            <input  type="hidden"  name="id" value="{{id}}">
            <input  type="hidden"  name="param_code" value="{{param_code}}">
            <input  type="hidden"  name="param_scope" value="{{param_scope}}">
            <input  type="hidden"  name="param_value" value="{{param_value}}">
            <table class="xw-table">
                <tr> <th width="80"></th><th></th></tr>
                <tr>
                    <td class="f-orage">名称</td>
                    <td><input  type="text" class="layui-input" name="param_text" value="{{param_text}}" datatype="*"></td>
                </tr>
                <!--<tr>
                    <td class="f-orage">选项值</td>
                    <td><input  type="text" class="layui-input" name="param_value" value="{{param_value}}" datatype="*"> </td>
                </tr>-->
            </table>
        </form>
    </script>     
    
</block>	

<block name="FOOTER">
  <script type="text/javascript">
  	var page=(function(ns){
	
		var selectGrid = null,delParamGrid=null;
		
	    //页面初始化
	    ns.init=function(){
	    	$(".btnAdd").click(function(){ ns.addParam(); });
	    	$(".btnDel").click(function(){ ns.delParam(); });
			$(".dlList dd").click(function(){
				ns.onClickParam($(this).data("data"));
			});
	    }
	    
	    //新增参数
	    ns.addParam = function(){
	    	var filter={
	            url:"system/setParam/edit",
	            oid:"",
	            mode:1
	        }
	        $.openIframe(filter, "新增参数",['520px', '450px']);
	    }
	    
	    //删除参数
	    ns.delParam = function(param_id){
	    	if(param_id && delParamGrid){
	    		delParamGrid.deleteSelectRow(function(){
	    			window.location.href = window.location.href;
	    		});
	    		return false;
	    	}
	    	function successFn(index,oLayer){
	    	    $(oLayer).find('.layui-layer-content').css("padding",'3px');
	            delParamGrid = layui.grid.render("delParamGrid");  //参2非必填，默认当前config.xml
	            delParamGrid.load([]);
	    	}
	    	function yesFn(index){
	            layer.close(index);
	        }
	    	var html = '<div id="delParamGrid" padding="0" height="100%"></div>';
	    	$.openModalDialog(html,"参数列表",successFn,yesFn,['850px', '450px']);
	    }
	    
	    //点击参数
	    ns.onClickParam=function(data){
	    	switch(data.param_type){
	    		case "radio":
	    			ns.radio(data,3);
	    			break;
	    		case "text":
	    			ns.textOrTextarea(data,3);
	    			break;
			    case "textarea":
			    	ns.textOrTextarea(data,2);
			        break;
			    case "number":
	                ns.textOrTextarea(data,4);
	                break;        
			    case "select":
					ns.select(data);
			        break;
			    case "slides":
	                ns.slides(data);
	                break;       
	            case "custom":
	                ns.custom(data);
	                break; 
			}
	    }
	    
	    //单选类型
	    ns.radio = function(data){
	    	$.ajaxs("/admin/soft/param/getTextParamValue",data).then(function(res){
		    	function successFn(index,layero){
		    		if(res.data == '1'){ layero.find("input[value='1']").prop("checked", true); }
					layui.form.render();
					layero.find('.layui-form-radio').eq(0).css("margin-right","20px");
		    	}
		    	function yesFn(index,layero){
	                data.param_value= layero.find("input:checked").val();
	                $.ajaxs("/admin/soft/param/saveTextParamValue",data,function(){
	                    $.toast(1,"保存成功！");
	                    layer.close(index);
	                });
		    	}
		    	var html = '<div class="layui-form" style="text-align: center;line-height: 45px;"><input  type="radio" lay-skin="primary" name="state" title="否" value="0" checked><input type="radio" lay-skin="primary" data-opera="=" name="state" title="是" value="1" ></div>';
		    	$.openModalDialog(html,data.param_name,successFn,yesFn,['250px', '150px']);
	        });
	    }
	    
	    // textarea类型
	    ns.textOrTextarea=function(data,mode){
	    	console.log(mode)
	    	$.ajaxs("/admin/soft/param/getTextParamValue",data).then(function(res){
		    	layer.prompt({
		            title:data.param_name,
		            formType: mode || 2,
		            offset: '20%',
		            value:res.data,
		            yes: function(index, layero){
	                    var value = layero.find(".layui-layer-input").val();
	                    if( mode==4 && !Number(value)  && value !=="0" ){
	                        $.toast(0,"请输入数字类型！");
	                        return false;
	                    }
	                    data.param_value=value;
	                    $.ajaxs("/admin/soft/param/saveTextParamValue",data,function(){
	                        $.toast(1,"保存成功！");
	                        layer.close(index);
	                    });
	                }
		        });
	        });
	    }
	    
	    //下拉
	    ns.select=function(data){
	        ns.selectData = data;
	    	function successFn(index,oLayer){
	    	    $(oLayer).find('.layui-layer-content').css("padding",'3px');
	    	    var filter=[{"field":"param_scope","opera":"=","value":data.param_scope}];  
	    	    filter.push({"field":"param_code","opera":"=","value":data.param_code})
	            selectGrid = layui.grid.render("selectGrid");  //参2非必填，默认当前config.xml
	            selectGrid.load(filter);    
	    	}
	    	function yesFn(index){
	            layer.close(index);
	        }
	    	var html = '<div id="selectGrid" padding="0" height="100%"></div>';
	    	$.openModalDialog(html,data.param_name,successFn,yesFn,['500px', '450px']);
	    }
	    
	    //下拉编辑 type:1 = 新增， type:2 = 编辑， type:3 = 删除，
	    ns.selectEdit = function (type){
	        if(type==3){
	            selectGrid.deleteSelectRow();
	            return false;
	        }
	        var  selectedRow = selectGrid.getSelectedRows()[0]; 
	        function successFn(index,oLayer){
	            $(oLayer).find("input[name='param_text']").bind("input propertychange",function(){
	               $(oLayer).find("input[name='param_value']").val( $(this).val() );
	            });
	        }
	        function yesFn(index){
	            var data = $(".J-selectType").serializeJson();
	            $(".J-selectType").validformIn(function(data){ 
	                $.ajaxs("/admin/soft/param/saveParams",data,function(res){
	                    $.toast(1,res.msg);
	                    selectGrid.load();
	                });
	                layer.close(index);
	            });
	        }
	        ns.selectData.id = 0;
	        var html= template("tpl-selectType",type ==1 ? ns.selectData : selectedRow);
	        $.openModalDialog(html,type ==1 ? "新增" : '编辑',successFn,yesFn,["320px","200px"]);
	    }
	    
	    //下拉移动  type: 1 = 上移 ,  type: 2 = 下移
	    ns.move = function(type){
	    	var grids = selectGrid;
	        var rows = grids.getRows();
	        var selectedRow=grids.getSelectedRows();
	        if(!selectedRow[0]){
	            $.toast(0,"请先选择数据！");
	            return;
	        }
	        if( selectedRow.length >1 ){
	            $.toast(0,"只能选择一条数据！");
	            return;
	        }
	        var coverId=-1;
	      	rows.forEach(function(h,index){
	      		if(selectedRow[0].id==h.id){
	      			if(type==1){
	      				if(rows[index-1]){
	      					coverId = rows[index-1].id;
	      				}
	      			}else{
	      				if(rows[index+1]){
	      					coverId = rows[index+1].id;
	      				}
	      			}
	      		}
	      	});
	        var param={ moveId:selectedRow[0].id, coverId:coverId}
	        $.ajaxs("/admin/soft/Param/sortMove",param,function(res){
	        	grids.load()
	        });
	    }
	    
	    //幻灯片类型
	    ns.slides = function(data){
	        function successFn(){
	            layui.uploads.init({
	                id:"upload",                           
	                fkGuid:'00000000-0000-0000-0000-000000000000',       
	                ywName:"参数设置",              
	                fkType:data.param_code,    
	                multiple:true,                                        
	                mode:1,
	                actionCode:0,               
	                showSwiper:true,         //显示轮播图
	                accept:'images'           //允许上传的文件,images,file,video（视频）、audio（音频）
	            });
	        }
	        function yesFn(index){
	            layer.close(index)
	        }
	        var html = '<div id="upload"></div>';
	        $.openModalDialog(html,data.param_name,successFn,yesFn,['600px', '450px'],{
	            btn:["关闭"],
	            offset:"35px"
	        });
	    }
	    
	    //自定义类型
	    ns.custom=function(data){
	    	if( !data.page_url ){
	    		$.toast(0,"页面路由不能为空！");
	    		return false;
	    	}
	    	data.url =  data.page_url;
	    	data.mode =  1;
	        $.openIframe(data,data.param_name, String(data.page_size).split(',') || ['520px', '450px']);
	    }
	    
	    return ns;
	}({}));
	
	$(function(){ page.init();  });
  </script>
</block>


